package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppActivityOpenVipBinding
import com.cgbl.cg.vm.mine.OpenVipViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.QuestionBean
import com.robot.base.model.VipBean
import com.robot.base.util.LiveDataEventManager

@Route(path = AppConstants.Router.Mine.A_MINE_OPEN_VIP)
class OpenVipActivity :
    GLBaseActivity<AppActivityOpenVipBinding, OpenVipViewModel>() {

    private lateinit var vipMenuAdapter: VipMenuAdapter

    private lateinit var vipQuestionAdapter: VipQuestionAdapter

    private lateinit var vipAdapter: VipAdapter

    override fun initComponents() {
        initTiterBar()
        initView()
        viewModel.refreshUserInfo()
        viewModel.getVipList()
        viewModel.qaPage()
    }

    override fun initViewObservable() {
        LiveDataEventManager.observerLoginSuccessEvent(this){
            viewModel.refreshUserInfo()
        }

        viewModel.uc.vipEvent.observe(this){
            it?:return@observe
            var visit = 0
            var cardCount = 0
            it.forEachIndexed { index, vipBean ->
                if(cardCount < (vipBean.cardCount?:0)){
                    cardCount = vipBean.cardCount?:0
                    visit = index
                }
            }
            vipAdapter.visit = visit
            vipAdapter.setNewInstance(it as MutableList<VipBean>?)
        }

        viewModel.uc.questionEvent.observe(this){
            it?:return@observe
            vipQuestionAdapter.setNewInstance(it as MutableList<QuestionBean>?)
        }
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView.text = "VIP会员"
        backView.setOnClickListener { finish() }
    }

    private fun initView() {
        vipAdapter = VipAdapter()
        binding.recyclerviewVip.apply {
            adapter = vipAdapter
        }
        vipAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.quanyi.set(vipAdapter.data[position].quanyi)
            viewModel.vipName.set(vipAdapter.data[position].cardName)
            viewModel.id.set(vipAdapter.data[position].id)
            viewModel.price.set(vipAdapter.data[position].cardPrice)
            viewModel.priceText.set("确认支付￥${vipAdapter.data[position].cardPrice}")
        }
        vipMenuAdapter = VipMenuAdapter()
        vipQuestionAdapter = VipQuestionAdapter()
        binding.recyclerviewTequan.apply {
            layoutManager = GridLayoutManager(this@OpenVipActivity, 3)
            adapter = vipMenuAdapter
        }

        binding.recyclerviewQuestion.apply {
            layoutManager = LinearLayoutManager(this@OpenVipActivity)
            adapter = vipQuestionAdapter
        }

        val menus = mutableListOf(
            VipMenuBean(R.mipmap.ic_vip_tequan_menu1, "全站VIP免费学", "无限学习"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu2, "VIP标识", "尊贵身份"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu3, "APP离线缓存", "随时随地学"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu4, "专属服务", "在线交流"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu5, "单付费教程", "专享折扣"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu6, "配套文件", "边学边练"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu7, "海量资源", "随意下载"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu8, "内部资料", "海量专享"),
            VipMenuBean(R.mipmap.ic_vip_tequan_menu9, "售后服务", "专属保障")
        )
        vipMenuAdapter.setNewInstance(menus)
    }


    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_open_vip

    override fun initVariableId(): Int = BR.viewModel


}